package com.zhaoxi.project.sys.controller;

import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.sys.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

// * 字典信息操作处理
@Tag(name = "系统数据字典")
@RestController
@RequestMapping("/sys/dict")
public class DictController extends BaseService {
  @Autowired
  private DictService dictService;

  //  获列表
  @PreAuthorize("@ss.hasPermi('sys:dict:list')")
  @GetMapping("/getList")
  public PageData getList(Map map) {
    PageData pd = this.getPageData(map);
    //如果 pageNum 和 pageSize 都是有值的，就是需要分页的
    if (pd.getLong("pageNum") != null && pd.getLong("pageSize") != null){
      this.startPage();
    }
    return this.responses(dictService.getList(pd));
  }


  //  获列表
  @Operation(summary = "通过code编码获取数据字典列表")
  @PreAuthorize("@ss.hasPermi('sys:dict:list')")
  @GetMapping("/dictGetListByCode")
  public PageData dictGetListByCode(Map map) {
    PageData pd = this.getPageData(map);
    return dictService.dictGetListByCode(pd);
  }

  //  获行数
  @PreAuthorize("@ss.hasPermi('sys:dict:list')")
  @GetMapping("/getCount")
  public PageData getCount(Map map) {
    PageData pd = this.getPageData(map);
    return dictService.getCount(pd);
  }

  //  新增
  @PreAuthorize("@ss.hasPermi('sys:dict:add')")
  @Log(title = "字典管理", businessType = BusinessType.INSERT)
  @PostMapping("/tabs")
  public PageData addTabs(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return dictService.addTabs(pd);
  }

  //  修改
  @PreAuthorize("@ss.hasPermi('sys:dict:edit')")
  @Log(title = "字典管理", businessType = BusinessType.UPDATE)
  @PutMapping("/tabs")
  public PageData editTabs(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return dictService.editTabs(pd);
  }

  //  新增
  @PreAuthorize("@ss.hasPermi('sys:dict:add')")
  @Log(title = "字典管理", businessType = BusinessType.INSERT)
  @PostMapping("/tree")
  public PageData addTree(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return dictService.addTree(pd);
  }

  //  修改
  @PreAuthorize("@ss.hasPermi('sys:dict:edit')")
  @Log(title = "字典管理", businessType = BusinessType.UPDATE)
  @PutMapping("/tree")
  public PageData editTree(@RequestBody Map map) {
    PageData pd = this.getPageData(map);
    return dictService.editTree(pd);
  }
  //  删除
  @PreAuthorize("@ss.hasPermi('sys:dict:del')")
  @Log(title = "字典管理", businessType = BusinessType.DELETE)
  @DeleteMapping("id/{id}")
  public PageData del(@PathVariable Long id) {
    return dictService.del(id);
  }
}
